﻿using FlowerShop.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.Services
{
    public class CartService
    {
        private readonly Model1 _context;

        public CartService()
        {
            _context = new Model1();
        }

        public void AddToCart(int userId, int productId, int quantity = 1)
        {
            var product = _context.Tgood.FirstOrDefault(p => p.G_id == productId);
            if (product == null || product.G_kuchun < quantity)
            {
                throw new Exception("商品库存不足！");
            }

            var existingCartItem = _context.Tshoppingcars.FirstOrDefault(c => c.t_id == userId && c.G_id == productId);

            if (existingCartItem != null)
            {
                existingCartItem.kuchun += quantity;
            }
            else
            {
                _context.Tshoppingcars.Add(new Tshoppingcars
                {
                    t_id = userId,
                    G_id = productId,
                    G_name = product.G_name,
                    jiage = product.G_xiaoshoujia,
                    chebenjia = product.G_chenbenjia,
                    kuchun = quantity
                });
            }

            product.G_kuchun -= quantity;
            _context.SaveChanges();
        }

        public void RemoveFromCart(int userId, int productId)
        {
            var cartItem = _context.Tshoppingcars.FirstOrDefault(c => c.t_id == userId && c.G_id == productId);
            if (cartItem != null)
            {
                _context.Tshoppingcars.Remove(cartItem);
                var product = _context.Tgood.FirstOrDefault(p => p.G_id == productId);
                if (product != null)
                {
                    product.G_kuchun += cartItem.kuchun;
                }
                _context.SaveChanges();
            }
        }

        public List<Tshoppingcars> GetCartItems(int userId)
        {
            return _context.Tshoppingcars.Where(c => c.t_id == userId).ToList();
        }

        public (decimal totalPrice, decimal totalProfit) CalculateCartTotals(int userId)
        {
            var cartItems = GetCartItems(userId);
            decimal totalPrice = cartItems.Sum(item => item.jiage * item.kuchun);
            decimal totalProfit = cartItems.Sum(item => (item.jiage - item.chebenjia) * item.kuchun);
            return (totalPrice, totalProfit);
        }

        public void ClearCart(int userId)
        {
            var cartItems = _context.Tshoppingcars.Where(c => c.t_id == userId).ToList();
            foreach (var item in cartItems)
            {
                var product = _context.Tgood.FirstOrDefault(p => p.G_id == item.G_id);
                if (product != null)
                {
                    product.G_kuchun += item.kuchun;
                }
                _context.Tshoppingcars.Remove(item);
            }
            _context.SaveChanges();
        }
    }
}
